package com.jingdianjichi.subject.infra.basic.util;

import com.alibaba.druid.filter.config.ConfigTools;


import java.security.NoSuchAlgorithmException;

/**
 * @BelongsProject: jc-club
 * @BelongsPackage: com.jingdianjichi.subject.infra.basic.service.util
 * @Author: liyajun
 * @CreateTime: 2025-07-08  00:24
 * @Description: 数据库加密
 * @Version: 1.0
 */
public class DruidEncryptUtil {

    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }

    public static String decrypt(String cipherText) throws Exception {
        String encrypt = ConfigTools.decrypt(publicKey, cipherText);
        System.out.println("decrypt:" + encrypt);
        return encrypt;
    }

    public static void main(String[] args) throws Exception {
       String encrypt = encrypt("Lyjtdbk285417");
       System.out.println("encrypt:" + encrypt);
       decrypt(encrypt);
    }
}
